<template>
  <div class="article-comment">
    <!-- 全部评论 -->
    <van-sticky offset-top="10.63333vw">
      <div class="title van-hairline--bottom">
        <span>全部评论 ({{article.comm_count}})</span>
        <span>0 点赞</span>
      </div>
    </van-sticky>
    <!-- 评论列表 -->
    <div class="list">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有评论了"
        @load="onLoad"
      >
        <div
          class="item van-hairline--bottom"
          v-for="comment in commentList"
          :key="comment.com_id"
        >
          <van-image
            round
            width="10vw"
            height="10vw"
            :src="comment.aut_photo"
          />
          <div class="info">
            <p>
              <span class="name">{{ comment.aut_name }}</span>
              <span class="zan"
                >{{ comment.like_count }} <geek-icon name="like2"
              /></span>
            </p>
            <p class="cont">{{ comment.content }}</p>
            <p>
              <span class="reply" @click="showReplyModel(comment)"
                >{{comment.reply_count}}回复 <i class="van-icon van-icon-arrow"></i
              ></span>
              <span class="time">{{ comment.pubdate | relativeTime }}</span>
            </p>
          </div>
        </div>
      </van-list>
    </div>
    <!-- 底部工具 -->
    <div class="footer van-hairline--top">
      <div class="input" @click="showInput=true"><i class="van-icon van-icon-edit"></i></div>
      <div class="btn" @click="scrollToCommentOrTop">
        <geek-icon name="comment"></geek-icon>
        <p>评论</p>
        <i v-if="article.comm_count">{{ article.comm_count }}</i>
      </div>
      <div class="btn" @click="likeArticle">
        <geek-icon
          :name="article.attitude === 1 ? 'like-sel' : 'like'"
        ></geek-icon>
        <p>点赞</p>
      </div>
      <div class="btn" @click="collectArticle">
        <geek-icon
          :name="article.is_collected ? 'collect-sel' : 'collect'"
        ></geek-icon>
        <p>收藏</p>
      </div>
      <div class="btn">
        <geek-icon name="share"></geek-icon>
        <p>分享</p>
      </div>
    </div>
    <!-- 二级回复弹层 -->
    <van-popup v-model="reply.open" position="right">
      <!-- 头 -->
      <van-nav-bar
        left-arrow
        @click-left="reply.open = false;currentComment={}"
        :title="`${currentComment.reply_count}条回复`"
      />

      <div class="reply-wrapper list">
        <van-list
          :immediate-check="false"
          v-model="reply.loading"
          :finished="reply.finished"
          finished-text="没有回复了"
          @load="loadReply"
        >
          <!-- 列表 -->
          <div class="item van-hairline--bottom" v-for="comment in reply.commentList" :key="comment.com_id">
            <van-image
              round
              width="10vw"
              height="10vw"
              :src="comment.aut_photo"
            />
            <div class="info">
              <p>
                <span class="name">{{ comment.aut_name }}</span>
                <span class="zan">{{ comment.like_count }} <geek-icon name="like2" /></span>
              </p>
              <p class="cont">{{comment.content}}</p>
              <p><span class="time" style="margin-left: 0">2小时内</span></p>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 底 -->
      <div class="footer van-hairline--top" style="position: static">
        <div class="input big" @click="showInput=true"><i class="van-icon van-icon-edit"></i></div>
        <div class="btn">
          <geek-icon name="collect"></geek-icon>
          <p>收藏</p>
        </div>
        <div class="btn">
          <geek-icon name="share"></geek-icon>
          <p>分享</p>
        </div>
      </div>
    </van-popup>
    <!-- 评论&回复 -->
    <van-popup v-model="showInput" position="bottom">
      <van-nav-bar left-arrow @click-left="showInput=false;" @click-right="commentArticle" title="评论文章" right-text="发表" />
      <van-field
        v-model="text"
        rows="3"
        autosize
        type="textarea"
        maxlength="100"
        :placeholder="currentComment.aut_name?`@${currentComment.aut_name}`: '请输入评论'"
        show-word-limit
      />
    </van-popup>
  </div>
</template>
<script>
import {
  getArticleCommentsByID,
  likeArticle,
  collectArticle,
  getCommentsByCommentID,
  commentArticleOrComment
} from '@/api/article'
export default {
  name: 'ArticleComment',
  props: ['article'],
  data () {
    return {
      showInput: false,
      text: '',
      loading: false,
      finished: false,
      offset: null,
      commentList: [],
      currentComment: {},
      reply: {
        open: false,
        offset: null,
        loading: false,
        finished: false
      }
    }
  },
  created () {
    // this.getArticleCommentsByID()
  },
  methods: {
    //   评论文章
    async commentArticle () {
      if (this.currentComment.aut_name) {
        const [err, data] = await commentArticleOrComment(this.currentComment.com_id, this.text, this.article.art_id)
        if (err) return this.$toast.fail(err)
        console.log(data)
        // 1.关闭窗口
        this.showInput = false
        // 2.将新的二级评价添加到reply.commentList中去
        this.reply.commentList.push(data.data.data.new_obj)

        // 3让二级评论数字加1
        this.currentComment.reply_count++
        // 4.清空数据
        this.text = ''
      } else {
        const [err, data] = await commentArticleOrComment(this.article.art_id, this.text)
        if (err) return this.$toast.fail(err)
        console.log(data)
        //   1.关闭窗口
        this.showInput = false
        // 2.将新的评论添加到commentList
        this.commentList.unshift(data.data.data.new_obj)
        this.article.comm_count++
        this.text = ''
      }
    },
    //
    loadReply () {},
    async showReplyModel (comment) {
      // 将当前的评论存储到了currentComment
      this.currentComment = comment
      // 1.让弹出层显示
      this.reply.open = true
      //   2.请求数据
      const [err, data] = await getCommentsByCommentID(
        comment.com_id,
        this.reply.offset
      )
      if (err) return this.$toast.fail(err)
      console.log(data)
      if (data.data.data.end_id === data.data.data.last_id) {
        //   没有更多的评论了
        this.reply.finished = true
      } else {
        this.reply.offset = data.data.data.end_id
      }

      this.reply.commentList = data.data.data.results
    },
    async collectArticle () {
      const [err] = await collectArticle()
      if (err) return this.$toast.fail(err)
      // 取反
      this.article.is_collected = !this.article.is_collected

      this.$toast.success(
        this.article.is_collected ? '收藏成功' : '取消收藏成功'
      )
    },
    async likeArticle () {
      const [err] = await likeArticle()
      if (err) return this.$toast.fail(err)
      // 点赞或者取消点赞效果
      if (this.article.attitude === 1) {
        this.article.attitude = -1
        this.$toast.success('取消点赞成功')
      } else {
        this.article.attitude = 1
        this.$toast.success('点赞成功')
      }
    },
    scrollToCommentOrTop () {
      // this.emit("")
      this.$emit('click-scroll')
    },
    async onLoad () {
      this.loading = true
      const [err, data] = await getArticleCommentsByID(
        this.article.art_id,
        this.offset
      )
      if (err) return this.$toast.fail(err)

      this.commentList.push(...data.data.data.results)

      if (data.data.data.end_id === data.data.data.last_id) {
        this.finished = true
      } else {
        //   为下次请求做准备。
        this.offset = data.data.data.last_id
      }
      this.loading = false
    },
    async getArticleCommentsByID () {
      const [err, data] = await getArticleCommentsByID(
        this.article.art_id,
        this.offset
      )
      if (err) return this.$toast.fail(err)
      //   如果相等，说明是最后一页了。
      if (data.data.data.end_id === data.data.data.last_id) {
        this.commentList = data.data.data.results
        this.finished = true
      } else {
        this.offset = data.data.data.last_id
      }
    }
  }
}
</script>
<style scoped lang="less">
.article-comment {
  .title {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    background: #fff;
    span {
      font-size: 16px;
      &:last-child {
        color: #ccc;
        font-size: 14px;
      }
    }
  }
  .list {
    padding: 0 16px;
    .item {
      display: flex;
      padding: 10px 0;
      .info {
        padding-left: 10px;
        flex: 1;
        p {
          margin: 0;
          .name {
            font-size: 16px;
          }
          .zan {
            font-size: 14px;
            float: right;
            color: #999;
            .geek-icon {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }
          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }
          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;
            .van-icon {
              position: relative;
              top: 1px;
            }
          }
          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }
}

.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 50px;
  background: #fff;
  display: flex;
  width: 100%;
  align-items: center;
  .input {
    &.big {
      width: 260px;
    }
    margin-left: 10px;
    width: 200px;
    height: 34px;
    background: @geek-gray-color;
    border-radius: 17px;
    line-height: 36px;
    padding-left: 10px;
    .van-icon {
      color: #999;
    }
  }
  .btn {
    flex: 1;
    text-align: center;
    position: relative;
    p {
      margin: 0;
      font-size: 10px;
    }
    .geek-icon {
      font-size: 18px;
    }
    i {
      height: 16px;
      min-width: 16px;
      padding: 0 3px;
      background: @geek-color;
      color: #fff;
      font-size: 10px;
      position: absolute;
      right: 0;
      top: -4px;
      line-height: 16px;
      border-radius: 8px;
      font-style: normal;
    }
  }
}

// 最下面加如下样式
.van-popup {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.reply-wrapper {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}

::v-deep .van-nav-bar__text {
  color: @geek-color;
}
::v-deep .van-field__control {
  background: @geek-gray-color;
  padding: 5px 10px;
  margin-bottom: 5px;
  border-radius: 4px;
}
</style>
